/*
 * Copyright 2010 Pierre-Yves Ricau
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.capiq.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import com.excilys.capiq.server.model.WrapEater;
import com.excilys.capiq.shared.NewWrapEater;
import com.excilys.capiq.shared.WrapRecipe;
import com.excilys.capiq.shared.WrapResource;

public class WrapServerResource extends ServerResource implements WrapResource {

	@Put
	@Override
	public void addWrapEater(NewWrapEater eater) {

		PersistenceManager pm = PMF.getPersistenceManager();

		try {
			Query q = pm.newQuery(WrapEater.class, "user == u");
			q.declareParameters("String u");
			List<WrapEater> alreadySavedWrapEaters = (List<WrapEater>) q.execute(eater.getUsername());

			for (WrapEater wrapEater : alreadySavedWrapEaters) {
				pm.deletePersistent(wrapEater);
			}

			if (eater.getQuantity() < 1) {
				throw new IllegalArgumentException("Cannot eat less than one wrap!");
			}

			WrapEater wrapEater = new WrapEater();

			wrapEater.setUser(eater.getUsername());
			wrapEater.setNumberOfWraps(eater.getQuantity());

			pm.makePersistent(wrapEater);

		} finally {
			pm.close();
		}
	}

	@Delete
	@Override
	public void removeWrapEater(String username) {
		PersistenceManager pm = PMF.getPersistenceManager();

		try {
			Query q = pm.newQuery(WrapEater.class, "user == u");
			q.declareParameters("String u");

			q.deletePersistentAll(username);
		} finally {
			pm.close();
		}

	}

	@Get
	@Override
	public WrapRecipe getRecipe() {

		PersistenceManager pm = PMF.getPersistenceManager();

		Map<String, Integer> wrapEatersMap = new HashMap<String, Integer>();
		int totalNumberOfWraps = 0;
		try {
			List<WrapEater> wrapEaters = (List<WrapEater>) pm.newQuery(WrapEater.class).execute();

			for (WrapEater wrapEater : wrapEaters) {
				wrapEatersMap.put(wrapEater.getUser(), wrapEater.getNumberOfWraps());
				totalNumberOfWraps += wrapEater.getNumberOfWraps();
			}
		} finally {
			pm.close();
		}

		WrapRecipe recipe = new WrapRecipe();

		recipe.setNumberOfWraps(totalNumberOfWraps);
		recipe.setSaladWeight(17 * totalNumberOfWraps);
		recipe.setSaladPackNb((int) Math.ceil(totalNumberOfWraps / 18.));
		recipe.setBearnaiseWeight((int) Math.ceil(totalNumberOfWraps * 13.4));
		recipe.setBearnaisePackNb((int) Math.ceil(totalNumberOfWraps / 18.));
		recipe.setCheeseNb(totalNumberOfWraps);
		recipe.setCheesePackNb((int) Math.ceil(totalNumberOfWraps / 9.));
		recipe.setTomatoNb(totalNumberOfWraps);
		recipe.setTortillaNb(totalNumberOfWraps);
		recipe.setTortillaPackNb((int) Math.ceil(totalNumberOfWraps / 6.));

		recipe.setChickenNuggetNb(totalNumberOfWraps * 2);
		recipe.setChickenNuggetPackNb((int) Math.ceil(totalNumberOfWraps / 6.));

		recipe.setWrapEaters(wrapEatersMap);

		return recipe;
	}
}
